package com.wuyoucar.service.impl;

import com.wuyoucar.common.ResultBean;
import com.wuyoucar.entity.TUser;
import com.wuyoucar.mapper.TUserInfoMapper;
import com.wuyoucar.mapper.TUserMapper;
import com.wuyoucar.service.InTUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

@Service
public class TUserServiceImpl implements InTUserService {
    @Autowired
    TUserMapper tUserMapper;
    @Autowired
    private TUserInfoMapper tUserInfoMapper;
    @Autowired
    StringRedisTemplate stringRedisTemplate;

    //修改用户个人信息
    @Override
    public ResultBean updateUserById(TUser tUser) {
        int n = tUserMapper.updateTUserById(tUser);
        if (n > 0) {
            return new ResultBean(200, "操作成功");
        } else {
            return new ResultBean(404, "操作成功");
        }
    }


    //修改手机号
    @Override
    public ResultBean updateTel(Integer userid, String newphone, String vcode) {
        if (vcode.equals(stringRedisTemplate.opsForValue().get("vcode"))) {
            int n = tUserMapper.updateTel(userid, newphone);
            if (n > 0) {
                return new ResultBean(200, "更改成功");
            } else {
                return new ResultBean(404, "更改失败");
            }
        } else {
            return new ResultBean(-1, "验证码输入错误");
        }

    }

    //设置密码
    @Override
    public ResultBean setpwd(String password, Integer userid) {

        if (tUserMapper.updatePwd(password, userid) > 0) {
            TUser tUser = tUserMapper.selectOneById(userid);
            return new ResultBean(200, "操作成功", tUser);
        } else {
            return new ResultBean(404, "操作失败");
        }

    }

    //修改密码
    @Override
    public ResultBean updatepwd(String password, Integer userid) {
        if (tUserMapper.updatePwd(password, userid) > 0) {
            return new ResultBean(200, "操作成功", tUserMapper.selectOneById(userid));
        } else {
            return new ResultBean(404, "操作失败");
        }

    }

    //设置用户名
    @Override
    public ResultBean updateUsername(Integer userid, String username) {
        if (tUserMapper.queryOneByName(username) != null) {
            return new ResultBean(300, "该用户名已被使用,操作失败");
        } else {
            int n = tUserMapper.updateUsername(userid, username);
            if (n > 0) {
                return new ResultBean(200, "操作成功", tUserMapper.selectOneById(userid));
            } else {
                return new ResultBean(404, "操作失败");
            }
        }
    }

    //查询用户个人信息
    @Override
    public ResultBean queryOneById(Integer userid) {
        ResultBean rb = new ResultBean();
        if (tUserMapper.queryOneById(userid) == null) {
            rb.setCode(404);
            rb.setMsg("查询失败");
            rb.setData(null);
        } else {
            rb.setCode(200);
            rb.setMsg("查询成功");
            rb.setData(tUserMapper.queryOneById(userid));
        }
        return rb;
    }

    @Override
    public ResultBean selectuserinfoById(Integer userid) {
        ResultBean rb = new ResultBean();
        if (tUserInfoMapper.selectUserinfoByuserid(userid) == null) {
            rb.setCode(404);
            rb.setMsg("查询失败");
            rb.setData(null);
        } else {
            rb.setCode(200);
            rb.setMsg("查询成功");
            rb.setData(tUserInfoMapper.selectUserinfoByuserid(userid));
        }
        return rb;
    }


}
